//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LinearRegression.h"
using namespace Cephei::QL::Math;
#include <gen/QL/Math/Array.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::CLinearRegression::CLinearRegression (Cephei::IMatrix<Double>^ x, Cephei::IVector<Double>^ y) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CoMatrix<Double>^ _Cx;
    CoVector<Double>^ _Cy;
    try
    {
#ifdef HANDLE
        _phLinearRegression = NULL;
#endif
        CoMatrix<Double>^ _Cx = safe_cast<CoMatrix<Double>^> (x);
        _Cx->Lock();
        INativeMatrix<Double>^ _NCIx = _Cx->getFeature (NativeFeature::Value);
        CDoubleMatrix^ _NCx = safe_cast<CDoubleMatrix^>(_NCIx);
        std::vector<std::vector<QuantLib::Real> >& _x = static_cast<std::vector<std::vector<QuantLib::Real> >&> (_NCx->GetReference ());
        CoVector<Double>^ _Cy = safe_cast<CoVector<Double>^> (y);
        _Cy->Lock();
        INativeVector<Double>^ _NCIy = _Cy->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCy = safe_cast<CDoubleVector^>(_NCIy);
        std::vector<QuantLib::Real>& _y = static_cast<std::vector<QuantLib::Real>&> (_NCy->GetReference ());
        _ppLinearRegression = new boost::shared_ptr<QuantLib::LinearRegression> (new QuantLib::LinearRegression ( _x,  _y ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
        if (_Cy != nullptr) _Cy->Unlock();
    }
}
Cephei::QL::Math::CLinearRegression::CLinearRegression (boost::shared_ptr<QuantLib::LinearRegression>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLinearRegression = NULL;
#endif
	_ppLinearRegression = &childNative;
    
}
Cephei::QL::Math::CLinearRegression::CLinearRegression (QuantLib::LinearRegression& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLinearRegression = NULL;
#endif
	_ppLinearRegression = new boost::shared_ptr<QuantLib::LinearRegression> (&childNative);
    
    _LinearRegressionOwner = owner;
    
}

Cephei::QL::Math::CLinearRegression::CLinearRegression (CLinearRegression^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLinearRegression = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLinearRegression = new boost::shared_ptr<QuantLib::LinearRegression> (copy->GetShared());
        
    }
}
Cephei::QL::Math::CLinearRegression::CLinearRegression (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLinearRegression = NULL;
#endif
	if (!t->IsSubclassOf(CLinearRegression::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CLinearRegression::CLinearRegression (QuantLib::Handle<QuantLib::LinearRegression>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLinearRegression = &childNative;
	_ppLinearRegression = &static_cast<boost::shared_ptr<QuantLib::LinearRegression>>(childNative.currentLink());
    
    _LinearRegressionOwner = owner;
}
Cephei::QL::Math::CLinearRegression::CLinearRegression (QuantLib::Handle<QuantLib::LinearRegression> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLinearRegression = &childNative;
	_ppLinearRegression = &static_cast<boost::shared_ptr<QuantLib::LinearRegression>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CLinearRegression::CLinearRegression (QuantLib::LinearRegression childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLinearRegression = NULL;
#endif
	_ppLinearRegression = new boost::shared_ptr<QuantLib::LinearRegression> (new QuantLib::LinearRegression (childNative));
    
}
#endif

Cephei::QL::Math::CLinearRegression::~CLinearRegression ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLinearRegression != NULL)
    {
	    delete _ppLinearRegression;
        _ppLinearRegression = NULL;
    }
}
Cephei::QL::Math::CLinearRegression::!CLinearRegression ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLinearRegression != NULL)
    {
	    delete _ppLinearRegression;
    }
}
QuantLib::LinearRegression& Cephei::QL::Math::CLinearRegression::GetReference ()
{
    if (_ppLinearRegression == NULL) throw gcnew NativeNullException ();
	return **_ppLinearRegression;
}
boost::shared_ptr<QuantLib::LinearRegression>& Cephei::QL::Math::CLinearRegression::GetShared ()
{
    if (_ppLinearRegression == NULL) throw gcnew NativeNullException ();
	return *_ppLinearRegression;
}
QuantLib::LinearRegression* Cephei::QL::Math::CLinearRegression::GetPointer ()
{
    if (_ppLinearRegression == NULL) throw gcnew NativeNullException ();
	return &**_ppLinearRegression;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LinearRegression>& Cephei::QL::Math::CLinearRegression::GetHandle ()
{
	if (_phLinearRegression == NULL)
	{
		_phLinearRegression = new Handle<QuantLib::LinearRegression> (*_ppLinearRegression);
	}
	return *_phLinearRegression;
}
#endif
bool Cephei::QL::Math::CLinearRegression::HasNative () 
{
	return (_ppLinearRegression != NULL);
}

Cephei::QL::Math::IArray^ Cephei::QL::Math::CLinearRegression::Coefficients::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppLinearRegression)->coefficients ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::CLinearRegression::Residuals::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppLinearRegression)->residuals ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::CLinearRegression::StandardErrors::get ()
{
    try
    {
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppLinearRegression)->standardErrors ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::ILinearRegression^ Cephei::QL::Math::CLinearRegression_Factory::Create (Cephei::IMatrix<Double>^ x, Cephei::IVector<Double>^ y)
{
    return gcnew CLinearRegression ( x,  y);
}
